package view
{

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.utils.ByteArray;
	
	import mx.core.UIComponent;
	
	import source.SwfObj;
	
	import spark.primitives.Rect;
	
	import utils.SwfParser;
	
	import view.bitmap.BitmapManager;
	import view.bitmap.BitmapMovieClip;
	import view.bitmap.BitmapMovieClipEvent;
	
	[Event(name="complete",type="flash.events.Event")]
	public class DisplayItem extends UIComponent
	{
		private var _swfObj:SwfObj;
		private var _className:String;
		private var _class:Class;
		private var _displayInstance:DisplayObject//BitmapMovieClip;
		private var _displayContainer:Sprite;
		/**
		 *	是否显示区域背景颜色 
		 */
		public var showRect:Boolean = false;
		/**
		 *	是否停在第一帧 
		 */
		public var stopToFrameOne:Boolean = false;
		/**
		 *	是否使用位图渲染 
		 */
		public var useBitmap:Boolean = false;
		
		/**
		 *	设置所属舞台 
		 * @param appStage
		 * 
		 */
		public static function setAppStage(appStage:Stage):void
		{
			BitmapManager.appStage = appStage;
		}
		
		public function DisplayItem()
		{
			super();
			_displayContainer = new Sprite();
			addChild(_displayContainer);
			addEventListener(Event.REMOVED_FROM_STAGE,onRemove);
		}
		
		private function onRemove(e:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE,onRemove);
			removeEventListener(Event.COMPLETE,onRemove);
			clearAll();
		}
		
		public function show(className:String,swfObj:SwfObj):void
		{
			if(swfObj==null)return;
			clearAll();
			_className = className;
			_swfObj = swfObj;
			_class = swfObj.getClass(className);
			onParserComplete();
		}
		
		private function onParserComplete():void
		{
			if(_class){
				if(useBitmap)
				{
					var testObj:Object = new _class();
					if(testObj is MovieClip)
					{
						_displayInstance = new BitmapMovieClip(_class);
						if(BitmapMovieClip(_displayInstance).bitmapStatus >= BitmapMovieClip.COMPLETING ){//位图已经完成至少一帧
							onFrameComplete(null);
						}else{//一帧都未完成
							_displayInstance.addEventListener(BitmapMovieClipEvent.DRAW_FRAME_COMP,onFrameComplete);
						}
					}else{
						_displayInstance = new _class();
						_displayInstance.cacheAsBitmap = true;
						onFrameComplete(null);
					}
				}else{
					_displayInstance = new _class();
					onFrameComplete(null);
				}
			}
			if(showRect)drawRect();
			function onFrameComplete(e:BitmapMovieClipEvent=null):void
			{
				if(_displayInstance==null)return;
				_displayInstance.removeEventListener(BitmapMovieClipEvent.DRAW_FRAME_COMP,onFrameComplete);
				_displayInstance.x = _displayInstance.y = 0;
				_displayContainer.addChild(_displayInstance);
				_displayInstance.x = -_displayInstance.getRect(_displayContainer).x;
				_displayInstance.y = -_displayInstance.getRect(_displayContainer).y;
				if(_displayInstance is BitmapMovieClip)
				{
					if(stopToFrameOne)BitmapMovieClip(_displayInstance).gotoAndStop(1);
					else BitmapMovieClip(_displayInstance).play();
				}else if(_displayInstance is MovieClip)
				{
					if(stopToFrameOne)MovieClip(_displayInstance).gotoAndStop(1);
					else MovieClip(_displayInstance).play();
				}
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		
		private function drawRect():void
		{
			graphics.clear();
			graphics.beginFill(0x000000,.2);
			graphics.drawRect(getRect(this).x,getRect(this).y,getRect(this).width,getRect(this).height);
			graphics.endFill();
		}
		
		private function clearAll():void
		{
			_class = null;
			_className = null;
			_swfObj = null;
			if(_displayInstance)
			{
				_displayContainer.removeChild(_displayInstance);
				if(_displayInstance is BitmapMovieClip)
				{
					BitmapMovieClip(_displayInstance).destroy();
				}
				_displayInstance = null;
			}
		}
		
		override public function set width(value:Number):void
		{
			_displayContainer.width = value;
		}
		
		override public function get width():Number
		{
			return _displayContainer.width;
		}
		
		override public function set height(value:Number):void
		{
			_displayContainer.height = value;
		}
		
		override public function get height():Number
		{
			return _displayContainer.height;
		}
		
		override public function get scaleX():Number
		{
			return _displayContainer.scaleX;
		}
		
		override public function set scaleX(value:Number):void
		{
			_displayContainer.scaleX = value;
		}
		
		override public function get scaleY():Number
		{
			return _displayContainer.scaleY;
		}
		
		override public function set scaleY(value:Number):void
		{
			_displayContainer.scaleY = value;
		}
	}
}